#!/usr/bin/perl
#save_folder.cgi

require './folderman-lib.pl';

&ReadParse();

my $fname = $in{'foldername'};
my $loc = $in{'location'};
my $emode = $in{'edit'};


if($emode eq 'create')
{
	error_setup($text{'create_folder_error'});
}

if ($emode eq 'property')
{
	error_setup($text{'edit_folder_error'});
}

my $mntroot = &get_mnt_root;
if ( $loc !~ /^$mntroot.+\//)
{
	&nas_syslog("folderman", "naslog_warning", 30006, "Directory invalid!");
	&error($text{'dir_invalid'});
}
if ( !(-d $loc))
{
	&nas_syslog("folderman", "naslog_warning", 30007, "Directory invalid!");
	&error($text{'dir_invalid'});
}

if ($in{'foldername'} =~ /[^A-Za-z0-9\x7f-\xff\+\_\-\(\)]/)
{
	&nas_syslog("folderman", "naslog_warning", 30012, "Foldername invalid!");
	&error(&text('foldername_invalid', $in{'foldername'}));
}
if ($emode =~ /create/)
{
	error_setup($text{'create_folder_error'});
	my $cmsk = 0000;
	if ($in{'attr'}=~ /r/) { $cmsk |= 0444; }
	if ($in{'attr'}=~ /w/) { $cmsk |= 0222; }
	if ($in{'attr'}=~ /e/) { $cmsk |= 0111; }
	my $oldmask = umask 0000;
	$suc = mkdir("$loc$fname", $cmsk);
	umask $oldmask;
	if (!$suc)
	{
		&nas_syslog("folderman", "naslog_warning", 30000, "Create folder $loc$fname failed");
		&error(&text('create_folder_fail', "$loc$fname"));
	}
	&nas_syslog("folderman", "naslog_info", 30003, "Create folder $loc$fname success!");
	&redirect("index.cgi?dir=$loc");

#	&header($text{create_folder_title}, "");
#	print "<hr>\n";
#	if ($suc)
#	{
#		print "<p>", &text('create_folder_ok', "$loc$fname"),"</p>\n";
#	}
#	else
#	{
#		print "<p>", &text('create_folder_fail', "$loc$fname"),"</p>\n";
#	}
}
if ($emode =~ /property/)
{
	error_setup($text{'edit_folder_error'});
	my $oname = $in{'oldname'};
	my $pmsk = 0000;
        my $mark = "a+";
	if ($in{'attr'}=~ /r/) { $pmsk |= 0444; $mark .="r"; }
	if ($in{'attr'}=~ /w/) { $pmsk |= 0222; $mark .="w"; }
	if ($in{'attr'}=~ /e/) { $pmsk |= 0111; $mark .="x"; }
	my $omask = umask 0000;
 	$ppp = $in{'subdir'};
        if ($ppp =~ /s/) {
	   `chmod -R a-rwx $loc$oname`;
           `chmod -R $mark $loc$oname`;
        }
        else{
#           `chmod a-rwx $loc$oname`;
#           `chmod $mark $loc$oname`;            
            $res2 = chmod $pmsk, "$loc$oname";
        }
	umask $omask;
	if (!res2)
	{
		&nas_syslog("folderman", "naslog_warning", 30001, "Change folder $loc$oname attributes failed");
		&error(&text('folder_attributes_fail', "$loc$oname"));
	}

	if (!($oname =~ /^($fname)$/))
	{
		$res1 = rename("$loc$oname", "$loc$fname");
	}
	else
	{
		$res1 = true;
	}

	if (!$res1)
	{
		&nas_syslog("folderman", "naslog_warning", 30002, "Rename folder $loc$fname failed");
		&error(&text('folder_rename_fail', "$loc$oname"));
	}
	&nas_syslog("folderman", "naslog_info", 30004, "Change folder $loc$oname attributes success");
	&redirect("index.cgi?dir=$loc");

#	&header($text{folder_property_title}, "");


#	print "<hr>\n";
#	if ($res1 && $res2)
#	{
#		print "<p>", &text('folder_property_ok', "$loc$oname"),"</p>\n";
#	}
#	else
#	{
#		print "<p>", &text('folder_property_fail', "$loc$oname"),"</p>\n";
#	}
}
#print "<hr>\n";

#&footer("list_folders.cgi?dir=$loc",  $text{'list_folders_return'});

